package com.sangmo.fts.process.storage.service;

import com.sangmo.boot.framework.common.errors.ServiceException;
import com.sangmo.boot.framework.common.errors.StandardErrors;
import com.sangmo.boot.framework.plugin.dataview.crud.service.BaseViewService;
import com.sangmo.boot.framework.plugin.dataview.data.IDataViewRepo;
import com.sangmo.fts.basic.service.FileUploadService;
import com.sangmo.fts.common.authority.service.AuthorityEntityService;
import com.sangmo.fts.process.storage.interfaces.CsfLadingBillCreation;
import com.sangmo.fts.process.storage.model.CsfAgreement;
import com.sangmo.fts.process.storage.model.CsfContractItem;
import com.sangmo.fts.process.storage.model.CsfLadingBill;
import com.sangmo.fts.process.storage.model.CsfLadingBillItem;
import com.sangmo.fts.process.storage.model.CsfLoan;
import com.sangmo.fts.process.storage.model.CsfStock;
import com.sangmo.fts.process.storage.model.CsfStockItem;
import com.sangmo.fts.process.storage.repo.CsfLadingBillRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class CsfLadingBillEntityService extends AuthorityEntityService<CsfLadingBill> {

    @Autowired
    private CsfLadingBillRepo csfLadingBillRepo;

    @Autowired
    private CsfLoanService csfLoanService;

    @Autowired
    private CsfAgreementService csfAgreementService;

    @Autowired
    private FileUploadService fileUploadService;

    @Autowired
    private CsfContractService csfContractService;

    @Autowired
    private CsfLadingBillItemEntityService csfLadingBillItemEntityService;

    @Override
    protected IDataViewRepo<CsfLadingBill> getRepo() {
        return csfLadingBillRepo;
    }

    @Override
    protected void onCreate(CsfLadingBill entity, Object bo) {
        super.onCreate(entity, bo);
        CsfLadingBillCreation creation = (CsfLadingBillCreation) bo;
        CsfLoan loan = csfLoanService.get(creation.getLoanId());
        CsfAgreement agreement = csfAgreementService.get(loan.getAgreementId());
        entity.setAgreementId(agreement.getId());
        entity.setApplyId(agreement.getBuyerId());
        entity.setBankId(loan.getBankId());
        entity.setRegulatorId(agreement.getStorageId());
    }

    @Override
    protected void afterCreated(CsfLadingBill entity, Object bo) {
        super.afterCreated(entity, bo);
        CsfLadingBillCreation creation = (CsfLadingBillCreation) bo;
        if (null != creation.getBillApplyDoc()) {
            fileUploadService.updateRef(creation.getBillApplyDoc(), ""+entity.getId(), "CsfLadingBill.Apply");
        }
        CsfLoan loan = csfLoanService.get(creation.getLoanId());
        List<CsfContractItem> contractItems = csfContractService.listItemsByContractId(loan.getContractId());
        List<CsfLadingBillItem> ladingBillItems = buildCsfLadingBillItems(entity.getId(), contractItems);
        csfLadingBillItemEntityService.saveAll(ladingBillItems);
    }

    @Override
    protected void onDelete(CsfLadingBill entity) {
        super.onDelete(entity);
        if (!CsfLadingBill.States.PENDING.equals(entity.getState())) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.csfLadingBill.remove.permit.disabled");
        }
    }

    private List<CsfLadingBillItem> buildCsfLadingBillItems(Long ladingBillId, List<CsfContractItem> contractItems) {
        List<CsfLadingBillItem> ladingBillItems = new ArrayList<>();
        for (CsfContractItem contractItem : contractItems) {
            CsfLadingBillItem item = CsfLadingBillItem.fromContractItems(contractItem);
            item.setBillId(ladingBillId);
            ladingBillItems.add(item);
        }
        return ladingBillItems;
    }
}
